package com.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dto.BizBaseItemDto;
import com.entity.BizBaseItemEntity;
import com.utils.BeanConvertUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author: Cxx
 * @Version 1.0
 * @create: 2025-09-15 11:26
 * @description: 分录服务基类
 **/
public class BizBaseItemServiceImpl<M extends BaseMapper<T>, T extends BizBaseItemEntity> extends ServiceImpl<M, T> implements IBizBaseItemService<T> {

    private final Class<T> itemEnityClass;

    public BizBaseItemServiceImpl() {
        itemEnityClass = currentModelClass();
    }

    /**
     * description: 基于所属单据头内码查询分录 <br>
     * version: 1.0 <br>
     * date: 2025/9/15 11:33 <br>
     * author: Cxx <br>
     *
     * @param headGuid 所属单据头内码
     * @return java.util.List<T> 分录
     */
    @Override
    public List<T> listByHeadGuid(String headGuid) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(BizBaseItemEntity.HEAD_GUID, headGuid);
        return list(queryWrapper);
    }

    @Override
    public List<T> listByHeadGuids(Collection<String> headGuids) {
        if (CollectionUtils.isEmpty(headGuids)) {
            return Collections.emptyList();
        }
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(BizBaseItemEntity.HEAD_GUID, headGuids);
        return list(queryWrapper);
    }

    /**
     * description:  基于所属单据头内码查询分录DTO <br>
     * version: 1.0 <br>
     * date: 2025/9/15 11:34 <br>
     * author: Cxx <br>
     *
     * @param headGuid 所属单据头内码
     * @param clazz    分录DTO类型
     * @return java.util.List<D> 分录DTO
     */
    @Override
    public <D extends BizBaseItemDto> List<D> listDtoByHeadGuid(String headGuid, Class<D> clazz) {
        List<T> entities = listByHeadGuid(headGuid);
        return BeanConvertUtil.copyToList(entities, clazz);
    }

    /**
     * description: 基于所属单据头内码删除分录 <br>
     * version: 1.0 <br>
     * date: 2025/9/15 11:36 <br>
     * author: Cxx <br>
     *
     * @param headGuid 所属单据头内码
     * @return boolean 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByHeadGuid(String headGuid) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(BizBaseItemEntity.HEAD_GUID, headGuid);
        return remove(queryWrapper);
    }

    /**
     * description:  批量保存名录DTO<br>
     * version: 1.0 <br>
     * date: 2025/9/15 11:40 <br>
     * author: Cxx <br>
     *
     * @param dtoList  分录DTO
     * @param headGuid 所属单据头内码
     * @return boolean 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public <D extends BizBaseItemDto> boolean saveOrUpdateBatchDto(List<D> dtoList, String headGuid) {
        if (CollectionUtils.isEmpty(dtoList)) {
            return false;
        }
        List<T> entities = BeanConvertUtil.copyToList(dtoList, itemEnityClass);
        for (T entity : entities) {
            entity.setHeadGuid(headGuid);
        }
        return saveOrUpdateBatch(entities);
    }

    /**
     * description: 批量保存名录DTO <br>
     * version: 1.0 <br>
     * date: 2025/9/16 20:13 <br>
     * author: Cxx <br>
     *
     * @param dtoList  分录DTO
     * @param headGuid 所属单据头内码
     * @return boolean 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public <D extends BizBaseItemDto> boolean saveBatchDto(List<D> dtoList, String headGuid) {
        if (CollectionUtils.isEmpty(dtoList)) {
            return false;
        }
        for (D dto : dtoList) {
            dto.setGuid(null);
        }
        return saveOrUpdateBatchDto(dtoList, headGuid);
    }

}

